import datetime
import queue
import re
import time

import pymysql as pymysql
from fake_useragent import UserAgent
from lxml import etree
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

import spiderConfig


class basketballFull():
    chrome_path = r'./chromedriver.exe'

    def __init__(self, url):
        chrome_options = Options()
        chrome_options.add_argument('--headless')
        chrome_options.add_argument('--disable-gpu')
        self.q = queue.Queue()
        self.win1_q = queue.Queue()
        self.driver = webdriver.Chrome(chrome_options=chrome_options)
        self.base_url = url
        self.__host = spiderConfig.host
        self.__port = spiderConfig.port
        self.__user = spiderConfig.user
        self.__password = spiderConfig.password
        self.__db = spiderConfig.db
        self.__charset = spiderConfig.charset
        self.lists = []
        self.dicts = {}
        self.windows_1 = 0
        self.windows_2 = 0
        self.s = ""
        self.ua = UserAgent()

    def sql_operate(self, lists):
        sql__conn = pymysql.connect(
            host=self.__host,
            port=self.__port,
            user=self.__user,
            password=self.__password,
            db=self.__db,
            charset=self.__charset
        )
        cursor = sql__conn.cursor()
        for single in lists:
            is_update_sql = "select * from tp_game where fx_id = %s"
            is_update = cursor.execute(is_update_sql, single['fx_id'])
            if is_update == 0:
                sql_inert = "replace  into tp_game values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"
                data = [
                    (single['id'], single['game_type'], single['match_name'], single['match_date'], single['home_team'],
                     single['away_team'], single['half_score'], single['final_score'], single['match_status'],
                     single['minutes'],
                     single['fx_id'], single['hc'], single['hc_home'], single['hc_away'], single['ou'], single['ou_o'],
                     single['ou_u'],
                     single['hc_half'], single['hc_home_half'], single['hc_away_half'], single['ou_half'],
                     single['ou_o_half'],
                     single['ou_u_half'], single['style'], single['sort'], single['create_time'], single['update_time'],
                     single['tag'])]
                print(data)
                print('正在插入中')
                try:
                    cursor.executemany(sql_inert, data)
                    sql__conn.commit()
                except pymysql.err.IntegrityError:
                    print('key重复')
                    pass
            else:
                update_datas = {
                    'minutes': single['minutes'], 'match_date': single['match_date'],
                    'half_score': single['half_score'],
                    'final_score': single['final_score'],
                    'match_status': single['match_status'], 'hc': single['hc'],
                    'hc_home': single['hc_home'], 'hc_away': single['hc_away'], 'ou': single['ou'],
                    'ou_o': single['ou_o'],
                    'ou_u': single['ou_u'], 'hc_half': single['hc_half'], 'hc_home_half': single['hc_home_half'],
                    'hc_away_half': single['hc_away_half'],
                    'ou_half': single['ou_half'],
                    'ou_o_half': single['ou_o_half'],
                    'ou_u_half': single['ou_u_half'], 'update_time': single['update_time'], 'style': single['style']
                }
                try:
                    sql = "update tp_game set"
                    update = ''.join(
                        [" {key} = '{values}',".format(key=key, values=values) for key, values in update_datas.items()])
                    wheres = " where fx_id = '{}'".format(single['fx_id'])
                    updats_sql = sql + update[:-1] + wheres
                    cursor.execute(updats_sql)
                    print('正在更新{}的数据'.format(single['fx_id']))
                    sql__conn.commit()
                except pymysql.err.ProgrammingError as e:
                    print("数据更新出错")
                    pass
        cursor.close()
        sql__conn.close()

    def get_html(self):
        try:
            self.driver.get(self.base_url)
            self.driver.implicitly_wait(10)
            self.driver.find_element(By.ID, 'buttonAll').click()
        except Exception:
            self.driver.quit()
            time.sleep(1)
            self.main()
        self.driver.find_element(By.XPATH, '//*[@id="selectCompany"]/option[4]').click()
        self.driver.implicitly_wait(10)
        complete_data = self.driver.page_source
        return complete_data

    # 解析出详情页url
    def parse_detail_url(self, complete_data):
        complete_html = etree.HTML(complete_data)
        total_tbody = complete_html.xpath('//table[@class="score-table"]/tbody')
        suspend_id = complete_html.xpath('//table[@class="score-table"]/tbody/tr[2]/td[15]/@id')
        window_1 = complete_html.xpath('//table[@class="score-table"]/tbody/tr[2]/@id')
        return total_tbody, suspend_id, window_1

    # 组装数据
    def assemble_data(self, total_tbody):
        for tbody in total_tbody:
            # 判断是否是精简
            is_hiddent = "".join(tbody.xpath('tr[5]/@style'))
            if str(is_hiddent) == "display:none":
                style = 0
            else:
                style = 1
            is_td_leng = len(tbody.xpath('tr[2]/td'))
            sid = self.q.get()
            tr_id = self.win1_q.get()
            match_name = "".join(tbody.xpath('tr[2]/td[2]/span/a/text()'))
            if not match_name:
                match_name = "".join(tbody.xpath('tr[2]/td[2]/span/text()'))
            match_date = "".join(tbody.xpath('tr[2]/td[3]/text()'))
            home_team_1 = "".join(tbody.xpath('tr[2]/td[4]/a/text()'))
            home_team_2 = "".join(tbody.xpath('tr[2]/td[4]/span/span/text()'))
            home_team = home_team_1 + home_team_2
            away_team_1 = "".join(tbody.xpath('tr[3]/td[1]/a/text()'))
            away_team_2 = "".join(tbody.xpath('tr[3]/td[1]/span/span/text()'))
            away_team = away_team_1 + away_team_2
            match_status = "".join(tbody.xpath('tr[1]/td[2]/span/span[1]/text()'))
            minutes_1 = "".join(tbody.xpath('tr[1]/td[2]/span[2]/span[1]/text()'))
            minutes_2 = "".join(tbody.xpath('tr[1]/td[2]/span[2]/span[2]/text()'))
            minutes = minutes_1 + minutes_2.strip()
            if not minutes:
                minutes = "".join(tbody.xpath('tr[1]/td[2]/span[2]/span/b/text()')).strip()
            home_half_score_1 = "".join(tbody.xpath('tr[2]/td[9]/font[1]/text()'))
            home_half_score_2 = "".join(tbody.xpath('tr[2]/td[9]/text()'))
            home_half_score_3 = "".join(tbody.xpath('tr[2]/td[9]/font[2]/text()'))
            home_half_score = home_half_score_1 + home_half_score_2 + home_half_score_3
            away_half_score_1 = "".join(tbody.xpath('tr[3]/td[6]/font[1]/text()'))
            away_half_score_2 = "".join(tbody.xpath('tr[3]/td[6]/text()'))
            away_half_score_3 = "".join(tbody.xpath('tr[3]/td[6]/font[2]/text()'))
            if away_half_score_1 and away_half_score_3:
                away_half_score = "-" + away_half_score_1 + away_half_score_2 + away_half_score_3
                half_score = home_half_score + "," + away_half_score
            else:
                half_score = ""
            home_final_score = "".join(tbody.xpath('tr[2]/td[10]/text()'))
            away_final_score = "".join(tbody.xpath('tr[3]/td[7]/text()'))
            if home_final_score and away_final_score:
                final_score = home_final_score + ',' + '-' + away_final_score
            else:
                final_score = ""
            hc = "".join(tbody.xpath('tr[2]/td[14]/a/span[1]/text()'))
            if not hc:
                hc = "".join(tbody.xpath('tr[3]/td[12]/a/span[1]/text()'))
            if hc:
                "-" + "".join(tbody.xpath('tr[3]/td[12]/a/span[1]/text()'))
            hc_home = "".join(tbody.xpath('tr[2]/td[14]/a/span[2]/text()'))
            hc_away = "".join(tbody.xpath('tr[3]/td[11]/a/span[2]/text()'))
            ou = "".join(tbody.xpath('tr[2]/td[15]/a/text()'))
            ou = "".join(ou).replace(" ", '').replace("大", "").replace("小", "").strip()
            ou = ou.split('/')[0]
            if not ou:
                ou = 0
            ou_o = "".join(tbody.xpath('tr[2]/td[15]/a/span/text()'))
            ou_u = "".join(tbody.xpath('tr[3]/td[12]/a/span/text()'))
            if is_td_leng != 16:
                home_half_score_1 = "".join(tbody.xpath('tr[2]/td[10]/font[1]/text()'))
                home_half_score_2 = "".join(tbody.xpath('tr[2]/td[10]/text()'))
                home_half_score_3 = "".join(tbody.xpath('tr[2]/td[10]/font[2]/text()'))
                home_half_score = home_half_score_1 + home_half_score_2 + home_half_score_3
                away_half_score_1 = "".join(tbody.xpath('tr[3]/td[7]/font[1]/text()'))
                away_half_score_2 = "".join(tbody.xpath('tr[3]/td[7]/text()'))
                away_half_score_3 = "".join(tbody.xpath('tr[3]/td[7]/font[2]/text()'))
                away_half_score = "-" + away_half_score_1 + away_half_score_2 + away_half_score_3
                half_score = home_half_score + "," + away_half_score
                home_final_score = "".join(tbody.xpath('tr[2]/td[11]/text()'))
                away_final_score = "".join(tbody.xpath('tr[3]/td[8]/text()'))
                final_score = home_final_score + ',' + '-' + away_final_score
                hc = "".join(tbody.xpath('tr[2]/td[15]/a/span[1]/text()'))
                if not hc:
                    hc = "".join(tbody.xpath('tr[3]/td[12]/a/span[1]/text()'))
                    if hc:
                        "-" + "".join(tbody.xpath('tr[3]/td[12]/a/span[1]/text()'))
                hc_home = "".join(tbody.xpath('tr[2]/td[15]/a/span[2]/text()'))
                hc_away = "".join(tbody.xpath('tr[3]/td[12]/a/span[2]/text()'))
                ou = "".join(tbody.xpath('tr[2]/td[16]/a/text()'))
                ou = "".join(ou).replace(" ", '').replace("大", "").replace("小", "").strip()
                ou = ou.split('/')[0]
                if not ou:
                    ou = 0
                ou_o = "".join(tbody.xpath('tr[2]/td[16]/a/span/text()'))
                ou_u = "".join(tbody.xpath('tr[3]/td[13]/a/span/text()'))
            is_hc_half_path = '//*[@id="{}"]/td[16]/a[2]/text()'.format(tr_id)
            is_ou_half_path = '//*[@id="{}"]/td[16]/a[3]/text()'.format(tr_id)
            is_win_1_path = "".join(tbody.xpath(is_hc_half_path))

            is_hc_xpath = 1
            hc_home_half, hc_half, hc_away_half = self.get_selenium_contorl(tr_id, is_win_1_path, is_hc_xpath)
            is_win_2_path = "".join(tbody.xpath(is_ou_half_path))
            is_ou_xpath = 2
            ou_o_half, ou_half, ou_u_half = self.get_selenium_contorl(tr_id, is_win_2_path, is_ou_xpath)
            # 将日期转换为时间戳
            times = str(time.time())[:10]
            current_date = str(datetime.date.today())[:5]
            match_date_1 = match_date.replace("月", "-").replace("日", " ")
            current_dates = current_date + match_date_1
            # 转为时间数组
            timeArray = time.strptime(current_dates, "%Y-%m-%d %H:%M")
            # 转为时间戳
            match_date = int(time.mktime(timeArray))
            # 浮点数不能为空
            if not ou:
                ou = 0
            if not ou_half:
                ou_half = 0
            self.dicts = {
                "id": 0,
                "game_type": 2,
                "match_name": match_name,
                "match_date": match_date,
                "home_team": home_team_1,
                "away_team": away_team_1,
                "half_score": half_score,
                "final_score": final_score,
                "match_status": 1,
                "minutes": minutes,
                "fx_id": sid,
                "hc": hc,
                "hc_home": hc_home,
                "hc_away": hc_away,
                "ou": ou,
                "ou_o": ou_o,
                "ou_u": ou_u,
                "hc_half": hc_half,
                "hc_home_half": hc_home_half,
                "hc_away_half": hc_away_half,
                "ou_half": ou_half,
                "ou_o_half": ou_o_half,
                "ou_u_half": ou_u_half,
                "style": style,
                "sort": 1,
                "create_time": times,
                "update_time": times,
                "tag": 0
            }
            print(self.dicts)
            self.lists.append(self.dicts)
        self.driver.close()
        return self.lists

    # hc_half窗口
    def get_selenium_contorl(self, tr_id, is_win_1_path, is_xpath):
        global is_window
        global is_win_xpath
        ret1 = ''
        ret2 = ''
        ret3 = ''
        if is_xpath == 1:
            if is_win_1_path:
                win1_xpath = '//*[@id="{}"]/td[16]/a[2]'.format(tr_id)
                is_win_xpath = win1_xpath
            else:
                win1_xpath = '//*[@id="{}"]/td[17]/a[2]'.format(tr_id)
                is_win_xpath = win1_xpath
        if is_xpath == 2:
            if is_win_1_path:
                win1_xpath = '//*[@id="{}"]/td[16]/a[3]'.format(tr_id)
                is_win_xpath = win1_xpath
            else:
                win1_xpath = '//*[@id="{}"]/td[17]/a[3]'.format(tr_id)
                is_win_xpath = win1_xpath
        try:
            self.driver.find_element(By.XPATH, is_win_xpath).click()
            windows_all = self.driver.window_handles
            if len(windows_all) == 2:
                is_window = 2
                time.sleep(1)
                self.driver.implicitly_wait(10)
                self.driver.switch_to.window(self.driver.window_handles[1])
                self.driver.find_element(By.XPATH, '//*[@id="tabs"]/a[2]').click()
                self.driver.implicitly_wait(10)
                complete_data = self.driver.page_source
                pattern = re.compile(
                    '''36\*.*?</td>.*?<td.*?oddstype="wholeLastOdds">.*?">(.*?)</font>.*?<td id=".*?" oddstype="wholeLastOdds">(.*?)</td>.*?<font color=".*?">(.*?)</font>''',
                    re.S)
                rets = re.findall(pattern, complete_data)
                if rets:
                    ret1 = str(rets[0][0]).strip()
                    if ret1 == '&nbsp;':
                        ret1 = ''
                    ret2 = str(rets[0][1]).strip()
                    if ret2 == '&nbsp;':
                        ret2 = ''
                    ret3 = str(rets[0][2]).strip()
                    if ret3 == '&nbsp;':
                        ret3 = ''
                self.driver.close()
                self.driver.switch_to.window(self.driver.window_handles[0])
                return ret1, ret2, ret3
        except Exception:
            print("操作太频繁了，请先歇一歇。")
            time.sleep(1)
            if is_window == 2:
                self.driver.close()
                self.driver.switch_to.window(self.driver.window_handles[0])
            return ret1, ret2, ret3

    def main(self):
        complete_data = self.get_html()
        total_tbody, suspend_id, window_1s = self.parse_detail_url(complete_data)
        for sid in suspend_id:
            self.q.put(sid)
        for window_1 in window_1s:
            self.win1_q.put(window_1)
        lists = self.assemble_data(total_tbody)
        self.sql_operate(lists)
        self.driver.quit()

if __name__ == '__main__':
    run = basketballFull('http://lq3.titan007.com/nba.htm')
    run.main()
